<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%!
	public void handleMap(List<Map> result ,String type ,String time){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for(Map map : result){
			map.put("type",type);
			map.put("statistical_time",time);
			map.put("create_time",sdf.format(new Date()));
			map.put("count",map.get("count")+"");
		}
		
	}
%>
<%
	//定时获取住院统计
	SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
	//最近的一次的更新时间
	String date = input.getDesc("date",sdf1.format(new Date()));
	String nowDate = input.getDesc("nowDate",sdf.format(new Date()));
	String nowEndDa = sdf.format(new Date());
	
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowDate+"24";
	String sqlWhere = "  " ;
	
	List<Map> oldList = QBuilder.db("basedb").queryHql("select new map(id as id ) from count_statistical where create_time='"+date+"'");
	if(oldList != null && oldList.size() > 0){
		//已经存在数据
		return;
	}
	String sql = "SELECT DISTINCT" +
        "hz.ksdm 代码," +
        "SUM (ryrs) 今日入院人数," +
        "SUM (cyrs) 今日出院人数," +
        "SUM (zyrs) 当前在院人数" +
        "FROM" +
        "BI_YZCX_ZYPT_HZ hz," +
        "BI_YZCX_ZYPT_MX mx," +
        "YY_KSBMK b (nolock)" +
        "WHERE" +
        "hz.bqdm COLLATE Chinese_PRC_BIN = mx.bqdm" +
        "AND hz.ksdm COLLATE Chinese_PRC_BIN = mx.ksdm" +
        "AND hz.tjrq = mx.tjrq" +
        "AND hz.tjrq LIKE '20170816'" +
        "AND hz.ksdm COLLATE Chinese_PRC_BIN = b.id" +
        "GROUP BY" +
        "hz.ksdm," +
        "b.name" +
        "ORDER BY" +
        "SUM (ryrs) DESC";
	List<Map> sumResult = QBuilder.db("basedb").queryHql(sql);
	
	List<Map> result1 = new ArrayList<Map>();
	List<Map> result2 = new ArrayList<Map>();
	List<Map> result3 = new ArrayList<Map>();
	
	
	if(result != null && result.size() != 0){
		handleMap(result,"1",nowDate);
		DBTools.db("localdb").addItem("count_statistical",result);
	}
	if(result2 != null && result2.size() != 0){
		handleMap(result2,"2",nowDate);
		DBTools.db("localdb").addItem("count_statistical",result2);
	}
	if(result3 != null && result3.size() != 0){
		handleMap(result3,"3",nowDate);
		DBTools.db("localdb").addItem("count_statistical",result3);
	}
	
	
	Message msg = new Message();
	

	out.println(msg);
%>